package com.company.project.controller;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.KeyValueUtils;
import com.company.project.entity.ShopEntity;
import com.company.project.service.ExhibitionGoodsService;
import com.company.project.service.ShopService;
import com.company.project.vo.KeyValueVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



/**
 * 商品购物
 *
 * @author HKX
 * @email *****@mail.com
 * @date 2022-05-01 11:14:43
 */
@Controller
@RequestMapping("/")
public class ShopController {
    @Autowired
    private ShopService shopService;

    @Autowired
    private ExhibitionGoodsService exhibitionGoodsService;

    @Value("${myUplodDir}")
    private String myUplodDir;
    /**
    * 跳转到页面
    */
    @GetMapping("/index/shop")
    public String shop() {
        return "shop/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("shop/add")
    @RequiresPermissions("shop:add")
    @ResponseBody
    public DataResult add(@RequestBody List<KeyValueVo> list){
        ShopEntity shopEntity = new ShopEntity();
        shopEntity.setId(IdUtil.simpleUUID());
        KeyValueVo keyValueVo = list.get(0);
        keyValueVo.setValue(shopEntity.getId());
        list.set(0,keyValueVo);
        shopEntity.setGoodsName(KeyValueUtils.findKey(list,"goodsName").getValue());
        shopEntity.setGoodsCount(KeyValueUtils.findKey(list,"goodsCount").getValue());
        shopEntity.setGoodsCategory(KeyValueUtils.findKey(list,"goodsCategory").getValue());
        shopEntity.setCreateTime(new Date());
        shopEntity.setUpdateTime(new Date());
        shopEntity.setGoodsStatus(Integer.valueOf(KeyValueUtils.findKey(list,"goodsStatus").getValue()) );
        String s = JSON.toJSONString(list);
        shopEntity.setContentJson(s);
        shopService.save(shopEntity);

//        ExhibitionGoodsEntity entity = new ExhibitionGoodsEntity();
//        entity.setId(IdUtil.simpleUUID());
//        entity.setGoodsName(shopEntity.getGoodsName());
//        entity.setGoodsCount(shopEntity.getGoodsCount());
//        entity.setGoodsCategory(shopEntity.getGoodsCategory());
//        entity.setGoodsId(shopEntity.getId());
//        exhibitionGoodsService.save(entity);

        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("shop/delete")
    @RequiresPermissions("shop:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        shopService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("shop/update")
    @RequiresPermissions("shop:update")
    @ResponseBody
    public DataResult update(@RequestBody List<KeyValueVo> list){
        ShopEntity shopEntity = new ShopEntity();
        shopEntity.setId(KeyValueUtils.findKey(list,"id").getValue());
        shopEntity.setGoodsName(KeyValueUtils.findKey(list,"goodsName").getValue());
        shopEntity.setGoodsCount(KeyValueUtils.findKey(list,"goodsCount").getValue());
        shopEntity.setGoodsCategory(KeyValueUtils.findKey(list,"goodsCategory").getValue());
        shopEntity.setGoodsStatus(Integer.valueOf(KeyValueUtils.findKey(list,"goodsStatus").getValue()) );
        String s = JSON.toJSONString(list);
        shopEntity.setContentJson(s);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date updateTime1=null;
        try {
            updateTime1 = simpleDateFormat.parse(KeyValueUtils.findKey(list, "updateTime").getValue());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        shopEntity.setUpdateTime(updateTime1);
        shopService.updateById(shopEntity);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("shop/listByPage")
    @RequiresPermissions("shop:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody ShopEntity shop){
        Page page = new Page(shop.getPage(), shop.getLimit());
        LambdaQueryWrapper<ShopEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        //queryWrapper.eq(ShopEntity::getId, shop.getId());
        if (StrUtil.isNotBlank(shop.getGoodsName())){
            queryWrapper.like(ShopEntity::getGoodsName,shop.getGoodsName()).or().like(ShopEntity::getGoodsCategory,shop.getGoodsCategory());
        }
        queryWrapper.orderByDesc(ShopEntity::getCreateTime);
        IPage<ShopEntity> iPage = shopService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

    @PostMapping("/on/pushOn/{id}")
    @ResponseBody
    public DataResult pushOn(@PathVariable("id") String  id){
        LambdaQueryWrapper<ShopEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopEntity::getId,id);
        ShopEntity byId = shopService.getOne(queryWrapper);
        if (byId.getGoodsStatus()==1){
            return new DataResult("fail");
        }
        byId.setGoodsStatus(1);
        shopService.updateById(byId);
        return new DataResult("ok");
//        System.out.println("hdckjlsacfkaksfnkc");
    }

    @PostMapping("/on/pullOn/{id}")
    @ResponseBody
    public DataResult pullOn(@PathVariable("id") String  id){
        LambdaQueryWrapper<ShopEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopEntity::getId,id);
        ShopEntity byId = shopService.getOne(queryWrapper);
        if (byId.getGoodsStatus()==0){
            return new DataResult("fail");
        }
        byId.setGoodsStatus(0);
        shopService.updateById(byId);
        return new DataResult("ok");
    }

    @ApiOperation(value = "上传文件")
    @PostMapping("/upload/dianziqianming")
    @ResponseBody
    public Object dianziqianming(@RequestParam("file") MultipartFile file){
        String fileName = null;
        try {
            String originalFilename = file.getOriginalFilename().trim();
            fileName = UUID.fastUUID()+ originalFilename.substring( originalFilename.lastIndexOf("."));//.png

            file.transferTo( new File(myUplodDir,fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return DataResult.success("/"+fileName);
    }
}
